package com.imay.live.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.live.R;
import com.imay.live.utils.ScreenSwitchUtils;
import com.imay.live.utils.ViewUtil;
import com.netease.nim.uikit.common.util.sys.ScreenUtil;

/**
 * Created by Administrator on 2017/9/13 0013.
 */

public class ViewFamilyProgress extends RelativeLayout {

    TextView tv_family_now_level, tv_family_next_level, tv_family_level_contribution;
    View v_progress, v_progress_max;
    RelativeLayout rl_progress;

    public ViewFamilyProgress(Context context) {
        this(context, null);
    }

    public ViewFamilyProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ViewFamilyProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_family_progress, this);
        rl_progress = ViewUtil.findView(this, R.id.rl_progress);
        v_progress_max = ViewUtil.findView(this, R.id.v_progress_max);
        v_progress = ViewUtil.findView(this, R.id.v_progress);
        tv_family_now_level = ViewUtil.findView(this, R.id.tv_family_now_level);
        tv_family_next_level = ViewUtil.findView(this, R.id.tv_family_next_level);
        tv_family_level_contribution = ViewUtil.findView(this, R.id.tv_family_level_contribution);
    }

    public void setNowLevel(int level) {
        tv_family_now_level.setText(getContext().getString(R.string.str_family_level, level));
    }

    public void setNextLevel(int level) {
        tv_family_next_level.setText(getContext().getString(R.string.str_family_level, level));
    }

    public void setContribution(int current, int count) {
        tv_family_level_contribution.setText(getContext().getString(R.string.str_family_level_text, current, count));
        if (current == 0) {
            RelativeLayout.LayoutParams layoutParams = (LayoutParams) v_progress.getLayoutParams();
            layoutParams.width = 0;
            v_progress.setLayoutParams(layoutParams);
        } else if (current == count) {
            RelativeLayout.LayoutParams layoutParams = (LayoutParams) v_progress.getLayoutParams();
            layoutParams.width = LayoutParams.MATCH_PARENT;
            v_progress.setLayoutParams(layoutParams);
        } else {
            float c = current * 1F / count * 1F;
            int width = ScreenUtil.screenWidth - ScreenUtil.dip2px(40);
            RelativeLayout.LayoutParams layoutParams = (LayoutParams) v_progress.getLayoutParams();
            layoutParams.width = (int) (c * width);
            v_progress.setLayoutParams(layoutParams);
        }
    }

    public void setMaxLevel() {
        tv_family_level_contribution.setText(R.string.str_family_level_max);
    }
}
